<?php
/*
 * Some functions for mainly admin hooks
 */

//admin password notices
add_action('admin_notices', 'abee_admin_notice');

function abee_admin_notice() {
    add_option('abee_passupdated', '0');
    if (current_user_can('administrator') && get_option('abee_passupdated') == '0')
        echo '<div class="updated">
       <p>Proszę o zmianę hasła <a href="profile.php">tutaj</a>.</p>
    </div>';
}

add_action('load-profile.php', 'abee_passupdate');

function abee_passupdate() {
    update_option('abeepassupdated', '1');
}

//meta  boxes for page editing, ex. if title of page should be visible
include_once 'customMeta.php';

add_action('add_meta_boxes', 'abee_metabox');
add_action('save_post', 'abee_savepostdata');

function abee_metabox() {
    add_meta_box('abee_metabox', 'Ustawienia dodatkowe', 'abee_renderMetabox', 'page', 'side', 'default');
}

function abee_savepostdata($post_id) {
//    if (!wp_verify_nonce($_POST['abee_nonce'], plugin_basename(__FILE__)))
//        return;
    // Check permissions
    if ('page' == $_POST['post_type']) {
        if (!current_user_can('edit_page', $post_id))
            return;
    }


    $newvalue = $_POST['abee_viewpagetitle'];
    // '_' mark before key means that custom field will be hidden - soo cool
    add_post_meta($post_id, '_abee_viewtitle', $newvalue, true) or update_post_meta($post_id, '_abee_viewtitle', $newvalue);
}

add_action('admin_print_styles', 'abee_adminStyles');

function abee_adminStyles() {
    if (current_user_can('editor')) {
        wp_enqueue_style('editorStyle', plugins_url('editorStyle.css', __FILE__));
    }
}

add_action('admin_init', 'fb_remove_postbox');

function fb_remove_postbox() {
    wp_deregister_script('postbox');
}

add_action('admin_print_scripts', 'abee_editorScript');

function abee_editorScript() {
    if (current_user_can('editor')) {
        wp_enqueue_script('editorScript', plugins_url('editorScript.js', __FILE__), array('jquery'));
    }
}

add_action('widgets_init', 'abee_widgets');

function abee_widgets() {
//    Remove widgets:
    unregister_widget('WP_Widget_Calendar');
    unregister_widget('WP_Widget_Archives');
    unregister_widget('WP_Widget_Links');
    unregister_widget('WP_Widget_Meta');
    unregister_widget('WP_Widget_Categories');
    unregister_widget('WP_Widget_Recent_Posts');
    unregister_widget('WP_Widget_Recent_Comments');
    unregister_widget('WP_Widget_RSS');
    unregister_widget('WP_Widget_Tag_Cloud');
}

add_filter('admin_footer_text', 'remove_footer_admin');

add_action('wp_head', 'abee_addGA');

function abee_addGA() {
    $ga_code = stripslashes(get_option('abee_gacode'));
    if (!empty($ga_code))
        echo $ga_code;
}

function remove_footer_admin() {
    echo 'Guarantee and support ABEE STUDIO. Powered by WordPress.';
}

function abeedashboard_styles() {
    //for editors
    if (current_user_can('editor')) {
        $abee_tempcolor = '#DFDFDF';


        $currentScreen = get_current_screen();
        ?>
        <?php if ($currentScreen->id == 'abee/dashboard') : ?>
            <style>
                html{
                    background-image:url('<?= plugins_url('images/background.jpg', __FILE__) ?>');
                }
            </style>
        <?php endif; ?>

        <style>
            html.wp-toolbar{
                padding-top: 0px !important;
            }

            #abeeheader{
                border-bottom: solid 1px <?php echo $abee_tempcolor; ?>;
                padding: 7px 0px;
            }
            #abeeheader a, #abeeheader span{
                color: <?php echo $abee_tempcolor; ?>;
                font-size: 15px;
                text-decoration: none;
            }
            #abeepagename{
                color: #282E6C;
                font-size: 34px;
                line-height: 49px;
                margin-left: 29px;
                margin-bottom: -32px;
            }
            #abeepagename img{
                position: relative;
                top: 8px;
                height: 39px;
            }
            .show-settings{
                color: #777;
                text-decoration: none;
                z-index: 1;
                padding: 0 16px 0 6px;
                height: 22px;
                line-height: 22px;
                font-size: 12px;
                display: block;
                text-shadow: rgba(255, 255, 255, 0.7) 0 1px 0;
                background: transparent url(images/arrows.png) no-repeat right 3px;
            }
            #footer{
                margin-left:20px;
            }
            #adminmenuback, #adminmenuwrap{display:none}

            #wpcontent, #footer {
                margin: 0px 20px;
            }
            .abeebox{
                background-color: #EEE;
                width: 240px;
                height: 240px;
                border: solid 1px #8E8E8E;
                background-repeat: no-repeat;
                background-position: 62px 30px;
            }
            .abeebox p {
                font-size: 24px;
                text-align: center;
                bottom: -148px;
                position: relative;
                line-height: 29px;
                color: #888888;
            }
            .abeeboxWrap{
                float: left;
                margin: 25px;
                padding: 3px;
                border: 2px solid transparent;
            }
            .abeeboxWrap a{
                text-decoration: none;
            }
            .abeeboxWrap:hover{
                border: 2px solid #21759B;
            }
            #dashboard-widgets-wrap{
                margin-left: auto;
                margin-right: auto;
                width: 98%;
            }
            #screen-meta-links{
                display:none;
            }
            #wpadminbar{
                display:none;
            }
            #abeemenu{
                padding:0px;
                margin: 0px;
                float: right;
                height: 23px;
            }
            #abeemenu li{
                margin: 0px;
            }
            #abeemenu li ul{
                margin: 1px 10px 0px 6px;
                padding: 6px;
                line-height: 22px;
                background: #EEE;
                border: solid 1px #777;
                border-top: solid 1px white;
                visibility: hidden;
                border-radius: 3px;
            }
            #abeemenu lu ul lu a{
                text-decoration: none;
            }
            #abeemenu li:hover ul{
                visibility: visible;
            }

            .wrap h2{
                color: #282E6C;
                font-size: 34px;
                line-height: 49px;
                margin-left: 29px;
                text-shadow: none;
                min-height: 117px;
            }

            div.updated p{
                text-align: center;
            }
        </style>
        <?php
    }
}

add_action('admin_head', 'abeedashboard_styles');

function abee_whereami() {
    global $submenu, $menu, $parent_file, $title;

    $root_url = 'abee/dashboard.php';
    $current_url = basename($_SERVER[REQUEST_URI]);
    $abee_path = array();

    $abee_path[] = array(
        'name' => 'Panel administracyjny',
        'url' => $root_url
    );

//           First step
//    echo $parent_file;
    if ($parent_file != $root_url) {
        foreach ($menu as $value) {
            if (isset($value[2]) && $value[2] == $parent_file) :
                $abee_path[] = array(
                    'name' => $value[0],
                    'url' => $value[2]
                );
                break;
            endif;
        }
    }

//           Second step
    if ($parent_file != $current_url) :
        if (isset($submenu[$parent_file])) :

            foreach ($submenu[$parent_file] as $value) {
                if ($value[2] == $current_url)
                    $abee_path[] = array(
                        'name' => $value[0],
                        'url' => $value[2]
                    );
            }
        endif;
    endif;

//    wyjatek dla post.php
    if ($title == 'Edytuj stronę') {
        $abee_path[] = array(
            'name' => $title,
            'url' => $current_url
        );
    }

    return $abee_path;
}

add_action('in_admin_header', 'abee_adminbar');

function abee_adminbar() {
    if (current_user_can('editor')):
        ?>

        <div id="abeeheader">
            <?php
            $abee_path = abee_whereami();
//            print_r($abee_path);
            foreach ($abee_path as $key => $value) {

                if (false === strpos($value['url'], '.php') || false !== strpos($value['url'], 'abee/'))
                    $value['url'] = 'admin.php?page=' . $value['url'];

                echo '<a href="' . $value['url'], '">' . $value['name'] . '</a>';
                if ($key + 1 != count($abee_path)) {
                    echo ' > ';
                }
            }
            ?>



            <span style="float:right"><a href="<?php echo wp_logout_url() ?>">Wyloguj</a></span>
        </div>
        <div class="clear"></div>
        <?php
    endif;
}
?>
